from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
import sys
import subprocess

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("执行外部脚本")
        self.setFixedSize(300, 200)

        # 创建按钮并设置点击事件
        self.button = QPushButton("运行外部脚本")
        self.button.clicked.connect(self.run_external_script)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.button)

        # 设置中心窗口
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def run_external_script(self):
        try:
            # 使用 subprocess.Popen 来执行外部脚本
            subprocess.Popen(["python", "external_script.py"], shell=True)
        except Exception as e:
            print(f"无法运行脚本: {e}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    app.exec()
